package com.kun.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.kun.entity.Recommended;
import com.kun.entity.RecommendedExample;
import com.kun.entity.RecommendedExample.Criteria;
import com.kun.mapper.cluster.ClusterRecommendedMapper;
import com.kun.mapper.master.custom.RecommendMapperWCustom;
import com.kun.service.RecommentService;
import com.kun.util.PaginationUtil;

/**
 * @description: 
 * @author: KM
 * 2018年5月2日 下午4:20:52
 */
@Service
public class RecommentServiceImpl implements RecommentService {
	
	@Autowired
	private ClusterRecommendedMapper clusterRecommendedMapper;
	
	@Autowired
	private RecommendMapperWCustom masterRecommendedMapper;
	
	
	/**
	 * @Description: 首页轮播
	 * @param: number请求数
	 */
	public List<Recommended> selectRecommendedforIndex(int number){
		RecommendedExample example = new RecommendedExample();
		Criteria c = example.createCriteria();
		c.andCirclepictureIsNotNull();
		example.setOrderByClause("to_sort desc,recommendedId desc");
		PaginationUtil pagination = new PaginationUtil(0,number);
		example.setPagination(pagination);
		List<Recommended> list = clusterRecommendedMapper.selectByExample(example);
		List<Recommended> returnList = new ArrayList<Recommended>();
		for(int i=0;i<list.size();i++){
			Recommended r = list.get(i);
			if(r.getUrl()!=null&&!"".equals(r.getUrl())){
				r.setRealUrl(r.getUrl());
			}else if(r.getAvtivityId()!=null){
				r.setRealUrl("../activity/"+r.getAvtivityId());
			}else if(r.getBookId()!=null){
				r.setRealUrl("../book/"+r.getBookId());
			}else if(r.getWishId()!=null){
				r.setRealUrl("../wish/"+r.getWishId());
			}else{
				r.setRealUrl("../index");
			}
			returnList.add(r);
		}
		return returnList;
	}
	
	
	/**
	 * @Description: 后台轮播管理列表
	 */
	@SuppressWarnings("rawtypes")
	public Map<String,Object> selectAllRecomment(int begin,int length,String jsonString){
		RecommendedExample bex = new RecommendedExample();
		bex.setOrderByClause("to_sort desc,recommendedId desc");
		//Criteria criteria = bex.createCriteria();
		if(!"".equals(jsonString)&&jsonString!=null&&jsonString.length()>0){
			JSONObject json = JSONObject.parseObject(jsonString);
			Map cMap = (Map)json;
			if (cMap != null && !cMap.isEmpty()) {
				Iterator it = cMap.keySet().iterator();
				while(it.hasNext()){
					String name = (String)it.next();
					if(!cMap.get(name).equals(null)){
						if(name.equals("type")){
							//criteria.andTypeidLike(cMap.get(name) + "%");
						}
					}
				}
			}
		}
			
		int allRecord = clusterRecommendedMapper.countByExample(bex);
		PaginationUtil pagination = new PaginationUtil((begin-1)*length,length,allRecord);
		bex.setPagination(pagination);
		List<Recommended> recommendedList = clusterRecommendedMapper.selectByExample(bex);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("allRecord", allRecord);
		resultMap.put("recommendedList", recommendedList);
		return resultMap;
	}
	
	public int insertRecommended(Recommended recommended){
		return masterRecommendedMapper.insertReturnPrimaryKey(recommended);
	}
	
	public int updateRecommendedByKey(Recommended recommended){
		return masterRecommendedMapper.updateByPrimaryKeySelective(recommended);
	}
	
	/**
	 * 更新整个
	 * */
	public int updateAllRecommendedByKey(Recommended recommended){
		return masterRecommendedMapper.updateByPrimaryKey(recommended);
	}
	
	public int deleteRecommendedByKey(int id){
		return masterRecommendedMapper.deleteByPrimaryKey(id);
	}

}
